{% extends "base.html" %}

{% block head %}
<style>
/* 液态玻璃页面标题 */
.page-title {
    background: var(--glass-bg);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--glass-border);
    border-radius: var(--border-radius);
    padding: 1rem 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--glass-shadow);
    position: relative;
    overflow: hidden;
}

.page-title::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.page-title h2 {
    color: #1C1C1E !important;
    text-shadow: 0 2px 4px rgba(0, 122, 255, 0.1);
    position: relative;
    z-index: 1;
    margin: 0;
    font-size: 1.5rem;
}

/* 液态玻璃卡片 */
.card {
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur) !important;
    -webkit-backdrop-filter: var(--glass-blur) !important;
    border: 1px solid var(--glass-border) !important;
    border-radius: var(--border-radius) !important;
    box-shadow: var(--glass-shadow) !important;
    transition: var(--transition) !important;
    position: relative;
    overflow: hidden;
    margin-bottom: 1.5rem !important;
}

.card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2) !important;
}

.card-body {
    position: relative;
    z-index: 1;
    color: #1C1C1E !important;
    padding: 1.5rem !important;
}

/* 液态玻璃按钮 */
.btn {
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    transition: var(--transition) !important;
    border-radius: 8px !important;
    font-weight: 600 !important;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15) !important;
}

.btn-primary {
    background: rgba(255, 255, 255, 0.1) !important;
    color: #1C1C1E !important;
    padding: 0.75rem 1.5rem !important;
}

.btn-primary:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.1) !important;
    color: #1C1C1E !important;
    padding: 0.75rem 1.5rem !important;
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-success {
    background: rgba(255, 255, 255, 0.1) !important;
    color: #1C1C1E !important;
    padding: 0.75rem 1.5rem !important;
}

.btn-success:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

.btn-outline-secondary {
    background: rgba(255, 255, 255, 0.1) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
    color: #1C1C1E !important;
    padding: 0.5rem 1rem !important;
}

.btn-outline-secondary:hover {
    background: rgba(255, 255, 255, 0.15) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #1C1C1E !important;
}

/* 液态玻璃徽章 */
.badge {
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    font-weight: 600 !important;
    transition: var(--transition) !important;
    padding: 0.5rem 0.75rem !important;
    border-radius: 6px !important;
}

.badge:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transform: translateY(-1px);
}

/* 液态玻璃模态框 */
.modal-content {
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur) !important;
    -webkit-backdrop-filter: var(--glass-blur) !important;
    border: 1px solid var(--glass-border) !important;
    border-radius: var(--border-radius) !important;
    box-shadow: var(--glass-shadow) !important;
}

.modal-header {
    background: rgba(255, 255, 255, 0.1) !important;
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border-bottom: 1px solid var(--glass-border) !important;
    color: #1C1C1E !important;
}

.modal-body {
    color: #1C1C1E !important;
}

.modal-footer {
    background: rgba(255, 255, 255, 0.05) !important;
    border-top: 1px solid var(--glass-border) !important;
}

/* 液态玻璃警告框 */
.alert {
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur) !important;
    -webkit-backdrop-filter: var(--glass-blur) !important;
    border: 1px solid var(--glass-border) !important;
    border-radius: var(--border-radius) !important;
    color: #1C1C1E !important;
    position: relative;
    overflow: hidden;
}

.alert::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.alert-info {
    border-left: 4px solid #17a2b8 !important;
}

.alert-success {
    border-left: 4px solid #28a745 !important;
}

.alert-danger {
    border-left: 4px solid #dc3545 !important;
}

/* 液态玻璃脚本信息卡片 */
.script-info {
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur) !important;
    -webkit-backdrop-filter: var(--glass-blur) !important;
    border: 1px solid var(--glass-border) !important;
    border-radius: var(--border-radius) !important;
    padding: 1.5rem !important;
    margin-bottom: 1.5rem !important;
    box-shadow: var(--glass-shadow) !important;
    transition: var(--transition) !important;
    position: relative;
    overflow: hidden;
}

.script-info::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.script-info:hover {
    transform: translateY(-5px);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2) !important;
}

.script-info h5 {
    margin-bottom: 1rem !important;
    color: #1C1C1E !important;
    font-weight: 600 !important;
    position: relative;
    z-index: 1;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    position: relative;
    z-index: 1;
}

.info-item {
    display: flex;
    flex-direction: column;
}

.info-label {
    font-size: 0.75rem;
    color: rgba(28, 28, 30, 0.7) !important;
    text-transform: uppercase;
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.info-value {
    font-size: 0.875rem;
    color: #1C1C1E !important;
    font-weight: 500;
}

/* 液态玻璃编辑器工具栏 */
.editor-toolbar {
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur) !important;
    -webkit-backdrop-filter: var(--glass-blur) !important;
    border: 1px solid var(--glass-border) !important;
    border-bottom: none !important;
    border-radius: var(--border-radius) var(--border-radius) 0 0 !important;
    padding: 1rem !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0 !important;
    position: relative;
    overflow: hidden;
}

.editor-toolbar::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    pointer-events: none;
    z-index: 0;
}

.editor-actions {
    position: relative;
    z-index: 1;
}

/* 简化的代码编辑器样式 */
.code-editor-container {
    position: relative;
    background: var(--glass-bg) !important;
    backdrop-filter: var(--glass-blur-light) !important;
    -webkit-backdrop-filter: var(--glass-blur-light) !important;
    border: 1px solid var(--glass-border) !important;
    border-radius: 0 0 var(--border-radius) var(--border-radius) !important;
    overflow: hidden;
}

.code-editor-container::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.05) 0%, rgba(255, 255, 255, 0.02) 100%);
    pointer-events: none;
    z-index: 0;
}

.code-textarea {
    width: 100%;
    min-height: 500px;
    padding: 1rem;
    border: none;
    outline: none;
    background: transparent;
    color: #1C1C1E;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 14px;
    line-height: 1.5;
    resize: vertical;
    position: relative;
    z-index: 1;
}

.code-textarea:focus {
    box-shadow: none;
}

/* 暗色模式适配 */
[data-theme="dark"] .page-title {
    background: rgba(0, 0, 0, 0.3);
    border-color: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .page-title h2 {
    color: #ffffff !important;
}

[data-theme="dark"] .card {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .card-body {
    color: #ffffff !important;
}

[data-theme="dark"] .script-info {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .script-info h5 {
    color: #ffffff !important;
}

[data-theme="dark"] .info-label {
    color: rgba(255, 255, 255, 0.7) !important;
}

[data-theme="dark"] .info-value {
    color: #ffffff !important;
}

[data-theme="dark"] .editor-toolbar {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .modal-content {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .modal-header {
    background: rgba(0, 0, 0, 0.4) !important;
    border-bottom-color: rgba(255, 255, 255, 0.1) !important;
    color: #ffffff !important;
}

[data-theme="dark"] .modal-body {
    color: #ffffff !important;
}

[data-theme="dark"] .modal-footer {
    background: rgba(0, 0, 0, 0.2) !important;
    border-top-color: rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .alert {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
    color: #ffffff !important;
}

[data-theme="dark"] .code-editor-container {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .code-editor-container::before {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.02) 0%, rgba(255, 255, 255, 0.01) 100%);
}

[data-theme="dark"] .code-textarea {
    color: #ffffff;
}
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- 页面标题 -->
    <div class="page-title">
        <h2><i class="fas fa-edit me-2"></i>编辑自定义脚本</h2>
    </div>

    <!-- 脚本信息卡片 -->
    <div class="script-info">
        <h5><i class="fas fa-info-circle me-2"></i>脚本信息</h5>
        <div class="info-grid">
            <div class="info-item">
                <span class="info-label">脚本名称</span>
                <span class="info-value">{{ script.name }}</span>
            </div>
            <div class="info-item">
                <span class="info-label">文件路径</span>
                <span class="info-value">{{ script.file_path }}</span>
            </div>
            <div class="info-item">
                <span class="info-label">上传时间</span>
                <span class="info-value">{{ script.upload_time.strftime('%Y-%m-%d %H:%M:%S') if script.upload_time else '未知' }}</span>
            </div>
            <div class="info-item">
                <span class="info-label">文件大小</span>
                <span class="info-value">
                    {% if script.file_size < 1024 %}
                        {{ script.file_size }} B
                    {% elif script.file_size < 1024 * 1024 %}
                        {{ "%.1f"|format(script.file_size / 1024) }} KB
                    {% else %}
                        {{ "%.1f"|format(script.file_size / (1024 * 1024)) }} MB
                    {% endif %}
                </span>
            </div>
        </div>
    </div>

    <!-- 编辑器容器 -->
    <div class="card">
        <div class="editor-toolbar">
            <div class="editor-actions">
                <button type="button" class="btn btn-outline-secondary me-2" onclick="formatCode()">
                    <i class="fas fa-magic me-1"></i>格式化
                </button>
                <button type="button" class="btn btn-primary" onclick="saveScript()">
                    <i class="fas fa-save me-1"></i>保存
                </button>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="code-editor-container">
                <textarea id="code-editor" class="code-textarea" placeholder="在此输入代码...">{{ script_content }}</textarea>
            </div>
        </div>
    </div>

    <!-- 保存状态提示 -->
    <div id="save-status" class="alert" style="display: none;">
        <span id="save-message"></span>
    </div>
</div>

<!-- 保存确认模态框 -->
<div class="modal fade" id="saveModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fas fa-save me-2"></i>保存确认
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p><i class="fas fa-question-circle me-1"></i>确定要保存对脚本的修改吗？</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="fas fa-times me-1"></i>取消
                </button>
                <button type="button" class="btn btn-primary" onclick="confirmSave()">
                    <i class="fas fa-check me-1"></i>确定保存
                </button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
// 格式化代码
function formatCode() {
    const textarea = document.getElementById('code-editor');
    const code = textarea.value;
    
    // 简单的Python代码格式化
    const lines = code.split('\n');
    let formattedLines = [];
    let indentLevel = 0;
    
    for (let line of lines) {
        line = line.trim();
        if (line === '') {
            formattedLines.push('');
            continue;
        }
        
        // 减少缩进的情况
        if (line.startsWith('return ') || line.startsWith('break') || line.startsWith('continue') || 
            line.startsWith('pass') || line.startsWith('raise ') || line.startsWith('except') || 
            line.startsWith('else:') || line.startsWith('elif ') || line.startsWith('finally:')) {
            indentLevel = Math.max(0, indentLevel - 1);
        }
        
        // 添加缩进
        const indent = '    '.repeat(indentLevel);
        formattedLines.push(indent + line);
        
        // 增加缩进的情况
        if (line.endsWith(':') && !line.startsWith('#')) {
            indentLevel++;
        }
    }
    
    textarea.value = formattedLines.join('\n');
    showSaveStatus('代码已格式化', 'success');
}

// 保存脚本
function saveScript() {
    const saveModal = new bootstrap.Modal(document.getElementById('saveModal'));
    saveModal.show();
}

// 确认保存
function confirmSave() {
    const textarea = document.getElementById('code-editor');
    const code = textarea.value;
    
    // 关闭模态框
    const saveModal = bootstrap.Modal.getInstance(document.getElementById('saveModal'));
    saveModal.hide();
    
    // 发送保存请求
    fetch('{{ url_for("edit_custom_script", script_id=script.id) }}', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            content: code,
        })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            showSaveStatus('脚本保存成功！', 'success');
            // 3秒后隐藏提示
            setTimeout(() => {
                hideSaveStatus();
            }, 3000);
        } else {
            showSaveStatus('保存失败: ' + data.message, 'danger');
        }
    })
    .catch(error => {
        showSaveStatus('保存失败: ' + error.message, 'danger');
    });
}

// 显示保存状态
function showSaveStatus(message, type) {
    const statusDiv = document.getElementById('save-status');
    const messageSpan = document.getElementById('save-message');
    
    statusDiv.className = `alert alert-${type} mt-3`;
    messageSpan.textContent = message;
    statusDiv.style.display = 'block';
}

// 隐藏保存状态
function hideSaveStatus() {
    document.getElementById('save-status').style.display = 'none';
}

// 键盘快捷键
document.addEventListener('keydown', function(e) {
    // Ctrl+S 保存
    if (e.ctrlKey && e.key === 's') {
        e.preventDefault();
        saveScript();
    }
});

// 自动调整文本框高度
document.addEventListener('DOMContentLoaded', function() {
    const textarea = document.getElementById('code-editor');
    
    // 自动调整高度
    function adjustHeight() {
        textarea.style.height = 'auto';
        textarea.style.height = Math.max(500, textarea.scrollHeight) + 'px';
    }
    
    // 监听输入事件
    textarea.addEventListener('input', adjustHeight);
    
    // 初始化高度
    adjustHeight();
});
</script>
{% endblock %} 